package com.hcy.rabbitmanagerapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.hcy.manager.ManagerDriverCarInfoClient;
import com.hcy.manager.dto.response.HomeCarAndDriverDTO;
import com.hcy.orderclient.OrderBaseClient;
import com.hcy.rabbitbasicmodel.dto.HomeData;
import com.hcy.rabbitbasicmodel.model.ResponseData;
import com.hcy.rabbitbasicmodel.model.ReturnResult;
import com.hcy.rabbitbasicoss.utils.AliYunOssUtils;
import com.hcy.rabbitbasicutils.model.GeoResponseDTO;
import com.hcy.rabbitbasicutils.utils.GaoDeUtils;
import com.hcy.rabbitbasicutils.utils.RedisUtils;
import com.hcy.rabbitmanagerapi.model.HomeAllDataDTO;
import com.hcy.userclient.client.UserInfoClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author huchenying
 * @Description 获取页面信息
 * @Date 2021/12/20 12:52
 **/
@RestController
@RequestMapping("/index")
@Slf4j
public class IndexController {

    @Autowired
    GaoDeUtils gaoDeUtils;
    @Autowired
    RedisUtils redisUtils;
    @Autowired
    OrderBaseClient orderBaseClient;
    @Autowired
    UserInfoClient userInfoClient;
    @Autowired
    ManagerDriverCarInfoClient managerDriverCarInfoClient;

    @PostMapping("/upload")
    public ReturnResult uploadImage(MultipartFile file){

        try {
            String s = AliYunOssUtils.uploadImg(file);
            log.info("上传图片成功 url = [{}]",s);
            return ReturnResult.success().setData(s);
        } catch (Exception e) {
            log.error("upload error");
            return ReturnResult.fail("图片上传失败");
        }
    }

    @GetMapping("/searchAddress")
    public ReturnResult searchAddress(String keyword){
        try {
            JSONArray pois = gaoDeUtils.searchAddress(keyword);
            return ReturnResult.success().setData(pois);
        } catch (Exception e) {
//            e.printStackTrace();
            log.error("e -> {}", ToStringBuilder.reflectionToString(e));
            return ReturnResult.fail("查询地址失败");
        }

    }

    /**
     * @Author huchenying
     * @Description  获取在线车辆经纬度
     * @Date 2022/5/14 22:00
     **/
    @GetMapping("/getAllCar")
    public ReturnResult getAllCar(){
        List<GeoResponseDTO> allCarGeo = null;
        try {
            allCarGeo = redisUtils.getAllCarGeo();
        } catch (Exception e) {
            log.error("获取车辆信息失败 e -> {}",ToStringBuilder.reflectionToString(e));
            return ReturnResult.fail("获取车辆信息失败");
        }
        return ReturnResult.success(allCarGeo);
    }

    /**
     * @Author huchenying
     * @Description  获取统计信息
     * @Date 2022/5/15 19:42
     **/
    @GetMapping("/getAllData")
    public ReturnResult getAllData(){
        HomeAllDataDTO homeAllDataDTO = new HomeAllDataDTO();
        // 拼接日期
        List<String> data = new ArrayList<>();
        LocalDate now = LocalDate.now(ZoneId.of("+8"));
        for(int i = 7;i > 0;i --){
            LocalDate localDate = now.minusDays(i);
            int monthValue = localDate.getMonthValue();
            int dayOfMonth = localDate.getDayOfMonth();
            data.add(monthValue + "月" + dayOfMonth + "日");
        }
        homeAllDataDTO.setDate(data);
        // 获取最近一周的订单
        Integer newOrders = 0;
        ResponseData<List<HomeData>> orders = orderBaseClient.getHomeData();
        if(orders.getCode().isSuccess()){
            List<Integer> orderCounts = new ArrayList<>();
            for (HomeData datum : orders.getData()) {
                newOrders += datum.getCount();
                orderCounts.add(datum.getCount());
            }
            homeAllDataDTO.setOrders(orderCounts);
        }else {
            homeAllDataDTO.setOrders(new ArrayList<>());
        }
        homeAllDataDTO.setNewOrder(newOrders);
        // 获取最近一周的用户新增量
        Integer allUserCount = 0;
        ResponseData<List<HomeData>> newUsers = userInfoClient.getNewUsers();
        if(newUsers.getCode().isSuccess()){
            List<Integer> counts = new ArrayList<>();
            for (HomeData datum : newUsers.getData()) {
                allUserCount += datum.getCount();
                counts.add(datum.getCount());
            }
            homeAllDataDTO.setUsers(counts);
        }else {
            homeAllDataDTO.setUsers(new ArrayList<>());
        }
        homeAllDataDTO.setNewUser(allUserCount);
        // 获取最近一周的司机新增量
        // 获取最近一周的 车辆
        Integer allCarCount = 0;
        Integer allDriverCount = 0;
        ResponseData<HomeCarAndDriverDTO> newCarsDrivers = managerDriverCarInfoClient.getNewCarsDrivers();
        if(newCarsDrivers.getCode().isSuccess()){
            List<Integer> cars = new ArrayList<>();
            List<Integer> drivers = new ArrayList<>();

            for (HomeData datum : newCarsDrivers.getData().getCar()) {
                cars.add(datum.getCount());
                allCarCount += datum.getCount();
            }
            for (HomeData datum : newCarsDrivers.getData().getDriver()) {
                drivers.add(datum.getCount());
                allDriverCount += datum.getCount();
            }
            homeAllDataDTO.setCars(cars);
            homeAllDataDTO.setDriver(drivers);
        }else {
            homeAllDataDTO.setCars(new ArrayList<>());
            homeAllDataDTO.setDriver(new ArrayList<>());
        }
        homeAllDataDTO.setNewCar(allCarCount);
        homeAllDataDTO.setNewDriver(allDriverCount);
        return ReturnResult.success(homeAllDataDTO);
    }

}
